/**
 * FileFind - ftp/shares indexing and search php engine
 * Module: persist-jpa20
 * Description: Persistence implementation with JPA 2.0
 * Copyright (c) 2003-2010 Rage Steel, www.gt2.ru
 */
package ru.ffind.persist.jpa20.model;

import static ru.ffind.persist.jpa20.model.QueryNames.FIND_DIRS_BY_PARENT;
import static ru.ffind.persist.jpa20.model.QueryNames.FIND_DIR_BY_NAME; 

import javax.persistence.AssociationOverride;
import javax.persistence.AssociationOverrides;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Transient;

import ru.ffind.common.model.Directory;
import ru.ffind.common.model.Entry;

import com.google.common.collect.ImmutableList;

/**
 * Directory implementation.
 *
 * FIXME Add flags
 * FIXME Add owner object
 *
 * @author Rage Steel
 */
@Entity(name = "Dirs")
@Table(name = "ff_dirs")
@NamedQueries({ 
	@NamedQuery(name = FIND_DIRS_BY_PARENT,
			query = "SELECT d FROM Dirs d WHERE d.parent = :parent") })
	@NamedQuery(name = FIND_DIR_BY_NAME,
			query = "SELECT d FROM Dirs d WHERE d.parent = :parent AND d.name = :name")
@AttributeOverrides({
	@AttributeOverride(name = "id",
			column = @Column(name = "dir_id")),
	@AttributeOverride(name = "name", 
			column = @Column(name = "dir_name")),
	@AttributeOverride(name = "datetime", 
			column = @Column(name = "dir_datetime"))
})
@AssociationOverrides({
	@AssociationOverride(name = "parent",
			joinColumns = @JoinColumn(name = "par_dir_id"))
})
public class DirImpl extends EntryImpl 
		implements Directory {

	private static final long serialVersionUID = -84356583638185534L;

    private Integer filesCount;

    private Integer dirsCount;

    private Long totalSize;

    @Column(name = "files_count")
    public Integer getFilesCount() {
        return filesCount;
    }

    public void setFilesCount(Integer filesCount) {
        this.filesCount = filesCount;
    }

    @Column(name = "dirs_count")
    public Integer getDirsCount() {
        return dirsCount;
    }

    public void setDirsCount(Integer dirsCount) {
        this.dirsCount = dirsCount;
    }

    @Override
    @Transient
    public long getCount() {
        return filesCount + dirsCount;
    }

    @Column(name = "total_size")
    public Long getTotalSize() {
        return totalSize;
    }

    public void setTotalSize(Long totalSize) {
        this.totalSize = totalSize;
    }

    @Override
    @Transient
    public boolean isError() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    @Transient
    public ImmutableList<Entry> getEntries() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
}
